package ciyuan.Player;

import ciyuan.Constants.PlayerDataType;
import ciyuan.DataManager.PlayerDataManager;
import ciyuan.Tasks.PlayerTaskManager;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

/**
 * @author 150149
 */

@Mod.EventBusSubscriber
public class MagicRecover {

    static {
        PlayerTaskManager.register(player -> {
            long magic = PlayerDataManager.getLong(player, PlayerDataType.MAGIC);
            long maxMagic = PlayerDataManager.getLong(player, PlayerDataType.MAX_MAGIC);
            if (magic < maxMagic) {
                long regenCount = ComputedPlayerInfo.getMagicRegen(player);
                PlayerDataManager.setServerPlayerData(player, PlayerDataType.MAGIC, magic + regenCount);
            }
        });
    }

    @SubscribeEvent
    public static void onPlayerRespawn(PlayerEvent.PlayerRespawnEvent event) {
        if (event.getPlayer() instanceof ClientPlayerEntity) {
            return;
        }
        long maxMagic = PlayerDataManager.getLong(event.getPlayer(), PlayerDataType.MAX_MAGIC);
        PlayerDataManager.setServerPlayerData(event.getPlayer(), PlayerDataType.TEMPERATURE, 37.0);
        PlayerDataManager.setServerPlayerData(event.getPlayer(), PlayerDataType.MAX_MAGIC, maxMagic);
    }

}
